﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class UIUnionFightBagItem : MonoBehaviour
    {
        public enum UnionFightItemType
        {
            Pre_Battle,//战斗前购买
            Battle_Ing,//战斗中购买
        }

        public GameObject icon;
        public GameObject add;
        public UILabel cdLabel;
        public GameObject cdMask;
        public GameObject[] cdObjs;
        public UILabel restNum;
        public UnionFightItemType type;
        [HideInInspector]
        public bool isHaveItem;
        public InstUFItem item;
        string itemName;
        bool cdFlag;
        float cdTime;
        const float CD = 60f;
        public void SetItem(InstUFItem item)
        {
            this.item = item;
            //icon.GetComponent<CommonIcon>().SetData(item.id, false, item.leftUseNum);
            if(item.leftUseNum > 0)
            {
                isHaveItem = true;
                icon.GetComponent<CommonIcon>().itemCount.text = item.leftUseNum + "";
            }
            icon.SetActive(isHaveItem);
            add.SetActive(!isHaveItem);
        }
        
        public void ResetBag()
        {
            isHaveItem = false;
            icon.GetComponent<CommonIcon>().itemCount.text = "";
            icon.SetActive(isHaveItem);
            add.SetActive(!isHaveItem);
        }

        public void ReduceItem()
        {
            int leftNum = --item.leftUseNum;;
            if(item.leftUseNum <= 0)
            {
                isHaveItem = false;
                leftNum = 0;
            }
            icon.SetActive(isHaveItem);
            add.SetActive(!isHaveItem);
            icon.GetComponent<CommonIcon>().itemCount.text = leftNum + "";
            cdTime = (float)item.useCD;
            cdFlag = true;
        }

        public void OnClickItem()
        {
            if (isHaveItem) 
            {
                if (cdFlag)
                {
                    string tip = TableTextBlo.GetText("field_hp_cd_tip");
                    BoxManager.CreatePopupTextBox(tip);
                    return;
                }
                UIUnionFightRunMap.GetNetData().SendCSUFPlayerUseItem(item.id);
            }
            else
            {
                if(type == UnionFightItemType.Pre_Battle)
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("union_fight_buyitem_preTips"));
                else
                    BoxManager.CreatePopupTextBox(TableTextBlo.GetText("union_fight_buyitem_Tips"));
            }
        }

        void Update()
        {
            if (cdFlag)
            {
                cdTime -= Time.deltaTime;
                SetCdMask(cdTime);
                if (cdTime <= 0)
                {
                    cdFlag = false;
                }
            }
        }

        void SetCdMask(float time)
        {
            for(int i = 0; i < cdObjs.Length; i++)
            {
                var script = cdObjs[i].GetComponent<UIUnionFightBagItem>();
                script.cdLabel.text = (int)time + "";
                if((int)time == 0)
                    script.cdLabel.text = "";
                script.cdMask.GetComponent<UITexture>().fillAmount = time / CD;
            }
        }
    }
}
